﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;

namespace Dynamo
{
    public class AdminPermissions
    {
        public static bool IgnoreSecurity
        {
            get
            {
                string val = DotNetNuke.Entities.Portals.PortalController.GetPortalSetting("Scripting.IgnoreSecurity", DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "false");
                if (val != null && val.Trim() != "" && val.Trim().ToLower() == "true") return true;
                return false;
            }
            set
            {
                DotNetNuke.Entities.Portals.PortalController.UpdatePortalSetting(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "Scripting.IgnoreSecurity", value.ToString(), true);
            }
        }
        public static bool AllowCLR
        {
            get
            {
                string val = DotNetNuke.Entities.Portals.PortalController.GetPortalSetting("Scripting.AllowCLR", DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "false");
                if (val != null && val.Trim() != "" && val.Trim().ToLower() == "true") return true;
                return false;
            }
            set
            {
                DotNetNuke.Entities.Portals.PortalController.UpdatePortalSetting(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "Scripting.AllowCLR", value.ToString(), true);
            }
        }

        public static bool ScriptingEnabled
        {
            get
            {
                string val = DotNetNuke.Entities.Portals.PortalController.GetPortalSetting("Scripting.Enabled", DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "false");
                if (val != null && val.Trim() != "" && val.Trim().ToLower() == "true") return true;
                return false;
            }
            set
            {
                DotNetNuke.Entities.Portals.PortalController.UpdatePortalSetting(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "Scripting.Enabled", value.ToString(), true);
            }
        }
        public static string EnabledPermissions
        {
            get
            {
                return DotNetNuke.Entities.Portals.PortalController.GetPortalSetting("Scripting.Permissions", DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "");
            }
            set
            {
                DotNetNuke.Entities.Portals.PortalController.UpdatePortalSetting(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "Scripting.Permissions", value.ToString(), true);
            }
        }


        private static List<System.Type> iPermissionTypeList = null;
        private static object _PLock = new object();
        public static List<System.Type> IPermissionTypeList
        {
            get {
                if (iPermissionTypeList != null) return iPermissionTypeList;
                lock (_PLock)
                {
                    List<System.Type> l = GetIPermissionTypes();
                    iPermissionTypeList = l;
                }
                return iPermissionTypeList;
            }
        }
        private static List<System.Type> GetIPermissionTypes()
        {
            var binFolder = new System.IO.DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("bin"));
            var list = binFolder.GetFiles("*.dll");
            var asmList = from fileInfo in list select Assembly.LoadFile(fileInfo.FullName);
            var TypeList = new System.Collections.Generic.List<System.Type>();
            TypeList.Add(typeof(System.Security.Permissions.FileIOPermission));
            TypeList.Add(typeof(System.Security.Permissions.ReflectionPermission));

            foreach (Assembly a in asmList.ToList())
            {
                try
                {
                    foreach (Type t in a.GetTypes())
                    {
                        if (t.IsPublic && t.IsSubclassOf(typeof(System.Security.IPermission)))
                            TypeList.Add(t);

                    }
                }
                catch (System.Exception e)
                {
                    string f = e.ToString();
                    //TODO: Determine some of the commone reasons why this fails, correct it
                }
            }
            return TypeList;
        }
    }
}